﻿/// <Summary> 
/// $Author: Nikhil Chinchwade 
/// This file contains classes representing performance monitoring counters
/// </Summary>
/// 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.Diagnostics;
//using Microsoft.WindowsAzure.ServiceRuntime;

namespace MindTree.PerfWorkbench.Azure.Monitor.Core
{
    /// <summary>
    /// This class represents different performance countersets
    /// </summary>
    public class CounterSets
    {
        private IDictionary<int, CounterSet> ctrSets = new SortedDictionary<int, CounterSet>();

        /// <summary>
        /// Dictionary of unique performance counter sets
        /// </summary>
        public IDictionary<int, CounterSet> CtrSets
        {
            get
            {
                return ctrSets;
            }
            set
            {
                ctrSets = value;
            }
        }

        /// <summary>
        /// Next Available index for a new counterset
        /// </summary>

        public int NextAvailableIndex
        {
            get
            {
                if (CtrSets.Keys.Count == 0)
                {
                    return 0;
                }
                return CtrSets.Keys.Last<int>() + 1;
            }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public CounterSets()
        {

        }

        /// <summary>
        /// Parameterised constructor
        /// </summary>
        /// <param name="paramCtrSets">List of unique performance counter sets</param>
        public CounterSets(IDictionary<int, CounterSet> paramCtrSets)
        {
            CtrSets = paramCtrSets;
        }


        /// <summary>
        /// Add a new counter set to the list
        /// </summary>
        /// <param name="paramAzPerfConf">List of performance Counters</param>
        /// <returns>Index of the new counterset</returns>
        public int AddCounterSet(IList<PerfCounter> paramAzPerfConf)
        {
            CounterSet set = new CounterSet(paramAzPerfConf);
            
            int index = NextAvailableIndex;
            set.Name = "CounterSet - " + index;
            set.Index = index;
            CtrSets.Add(index, set);
            return index;
        }

        /// <summary>
        /// Check whether the counterset already exists
        /// </summary>
        /// <param name="paramCntrs">List of Performance Counters</param>
        /// <returns>Boolean</returns>
        public bool ContainsCntrSet(IList<PerfCounter> paramCntrs)
        {
            IEnumerable<int> i = from ctr in CtrSets where ctr.Value.Counters.SequenceEqual(paramCntrs) select ctr.Key;            
            
            if (i.Count<int>() <= 0)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// Get the countersetindex
        /// </summary>
        /// <param name="paramAzPerfConf"></param>
        /// <returns></returns>
        public int GetCountersetIndex(IList<PerformanceCounterConfiguration> paramAzPerfConf)
        {
            IList<PerfCounter> ctrs = GetCounters(paramAzPerfConf);
            if (ContainsCntrSet(ctrs))
            {
                IEnumerable<int> i = from ctr in CtrSets where ctr.Value.Counters == ctrs select ctr.Key;
                return i.FirstOrDefault<int>();
            }
            else
            {
                return AddCounterSet(ctrs);
            }
        }
        
        /// <summary>
        /// Get the counters from the CounterConfiguration list
        /// </summary>
        /// <param name="paramAzPerfConf">Performance Counter Configuration</param>
        /// <returns>List of performance Counters</returns>
        public static IList<PerfCounter> GetCounters(IList<PerformanceCounterConfiguration> paramAzPerfConf)
        {
            IList<PerfCounter> cntrs = new List<PerfCounter>();
            foreach (PerformanceCounterConfiguration cntr in paramAzPerfConf)
            {
                cntrs.Add(PerfCounter.GetCounterFromString(null, cntr.CounterSpecifier));
            }
            return cntrs;
        }

        /// <summary>
        /// Get the counterset from the index
        /// </summary>
        /// <param name="index">Index</param>
        /// <returns>Counterset Object</returns>
        public CounterSet GetCounterSet(int index)
        {
            if (CtrSets.Count > index && index >= 0)
            {
                return CtrSets[index];
            }
            return null;
        }
    }
}
